package ArrayList;

import org.junit.Test;

/**
 * @Classname JZ29顺时打印矩阵
 * @Description TODO
 * @Date 2022/12/6 17:24
 * @Created by xjl
 */
public class JZ29顺时打印矩阵 {

    /**
     * @description 直接模拟遍历
      * @param: matrix
     * @date: 2022/12/6 23:17
     * @return: int[]
     * @author: xjl
    */
    public int[] spiralOrder(int[][] matrix) {
        // 判断如果二维数组为空的话，那就可以使用直接判断返回
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new int[0];
        }
        int rows = matrix.length, columns = matrix[0].length;
        // 设置表示是否访问过了
        boolean[][] visited = new boolean[rows][columns];
        int[] result = new int[matrix.length * matrix[0].length];
        // 初始的位置
        int row = 0, column = 0;
        int total = rows * columns;
        // 表示四个方向的位置
        int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int directionIndex = 0;
        // 总共访问的还是m*n的个数
        for (int i = 0; i < total; i++) {
            // 放置当前的数据
            result[i] = matrix[row][column];
            // 表示已经访问过了
            visited[row][column] = true;
            // 下一个的方向
            int nextRow = row + directions[directionIndex][0];
            int nextColumn = column + directions[directionIndex][1];
            // 判断是否符合要求
            if (nextRow < 0 || nextRow >= rows || nextColumn < 0 || nextColumn >= columns || visited[nextRow][nextColumn]) {
                directionIndex = (directionIndex + 1) % 4;
            }
            row += directions[directionIndex][0];
            column += directions[directionIndex][1];
        }
        return result;
    }

    /**
     * @description 按照层遍历
      * @param: matrix
     * @date: 2022/12/6 23:17
     * @return: int[]
     * @author: xjl
    */
    public int[] spiralOrder2(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return new int[0];
        }
        int rows = matrix.length, columns = matrix[0].length;
        int[] order = new int[rows * columns];
        int index = 0;
        // 标记为四个点
        int left = 0, right = columns - 1, top = 0, bottom = rows - 1;
        while (left <= right && top <= bottom) {
            // 开始右边的方向
            for (int column = left; column <= right; column++) {
                order[index++] = matrix[top][column];
            }
            // 右边向下的方式 起始位置 top+1
            for (int row = top + 1; row <= bottom; row++) {
                order[index++] = matrix[row][right];
            }
            if (left < right && top < bottom) {
                // 向左边的下面
                for (int column = right - 1; column > left; column--) {
                    order[index++] = matrix[bottom][column];
                }
                // 左边的向上的方向
                for (int row = bottom; row > top; row--) {
                    order[index++] = matrix[row][left];
                }
            }
            // 然后开始到内圈
            left++;
            right--;
            top++;
            bottom--;
        }
        return order;
    }

    @Test
    public void test(){
        int[][] array=new int[][]{{1,2,3},{4,5,6},{7,8,9}};
        int[] ints = spiralOrder(array);

    }
}
